define(['common'], () => {
    /* 
        1. 点击登录  发送用户名和密码给后端  后端验证登录信息是否正确 返回是否登录成功  
                 模拟 登录  信息放在本地的  
        
        */
    class Login {
        constructor() {
            this.user = document.querySelector('#recipient-name')
            this.password = document.querySelector('#message-text')
            this.login = document.querySelector('.login')
            this.memory = document.querySelector('input[type=checkbox]')
            this.user_span = document.querySelector('.user_span')
            this.password_span = document.querySelector('.password_span')
            this.isEqual = false
            this.reg1 = /^[\u2E80-\u9FFF]+$/
            this.reg2 = /^[a-zA-Z0-9_]{6,16}$/

            this.onBlur()
            this.handleClick()
            this.changeValue()

        }
        onBlur() {

            this.user.onblur = () => {
                if (this.user.value == '') {
                    this.user_span.className = 'false'
                    this.user_span.innerHTML = '用户名不能为空'
                    return false
                } else if (this.reg1.test(this.user.value)) {
                    this.user_span.className = 'true'
                    this.user_span.innerHTML = '√'
                    return true
                } else {
                    this.user_span.className = 'false'
                    this.user_span.innerHTML = '用户名格式不正确'
                    return false
                }
            }
            this.password.onblur = () => {
                if (this.password.value == '') {
                    this.password_span.className = 'false'
                    this.password_span.innerHTML = '密码不能为空'
                    return false
                } else if (this.reg2.test(this.password.value)) {
                    this.password_span.className = 'true'
                    this.password_span.innerHTML = '√'
                    return true
                } else {
                    this.password_span.className = 'false'
                    this.password_span.innerHTML = '密码格式不正确'
                    return false
                }
            }
        }
        handleClick() {
            this.login.onclick = () => {
                let flag = this.reg1.test(this.user.value) == true && this.reg2.test(this.password.value) ==
                    true
                if (flag) {
                    let arr = JSON.parse(localStorage.getItem('info')) || []
                    if (arr.length == 0) {
                        alert('该用户还未注册，请到注册页面注册')
                        setTimeout((res) => {
                            location.href = '../html/register.html'
                        }, 2000)
                    } else {
                        let type = this.memory.checked
                    if (type) {
                        setCookie('username', this.user.value)
                        setCookie('password', this.password.value)
                        setCookie('select', type)
                    } else {
                        //无论是否7天免登陆，用户名在首页都要用到，所以必须存储
                        setCookie('username', this.user.value)
                        delCookie('password')
                        delCookie('select')
                    }
                        console.log(this.user.value);
                        console.log(this.password.value);
                        arr.forEach(item => {
                            if (item.username == this.user.value && item.password == this.password
                                .value) {
                                this.isEqual = true
                                alert('登陆成功，即将跳转')
                                location.href = '../index.html'
                                return
                            }else{
                                alert('用户名或密码错误！')
                                return
                            }
                        });
                    }
                    
                    
                }
            }

        }
        changeValue() {

            //注意点：如果页面里面没有cookie,获取的cookie的值就是undefined，并要把文本框和密码框的值清空
            if (getCookie('username') == undefined || getCookie('password') == undefined) {
                this.user.value = ''
                this.password.value = ''
            } else{
                //如果页面有cookie，应该把cookie值获取出来给文本框和密码框（复选框的状态也要赋值）
                this.user.value = getCookie('username')
                this.password.value = getCookie('password')
                this.memory.checked = getCookie('select')

            }

        }

    }
    new Login()
})